/* Copyright Andy Mitchell 2007. No warranties */

function GTDInboxPage(gtdinboxBrowser, doc){
try{

	this.id = (new Date()).valueOf();
	this.gtdinboxBrowser = gtdinboxBrowser;
	this.db = gtdinboxBrowser.db;
	this.sp = new StoredProceduresGTD(this);
	this.settings = gtdinboxBrowser.settings;
	this.eventObservers = new EventObservers(this);
		
	this.doc = doc;
	this.window = doc.defaultView;
	this.unsafeWindow = this.window.wrappedJSObject;
	this.attachToBrowser();
		
	this._gtdFrame = this.doc.location.href.match(/gtd\=(\d+)/i);
	if( this._gtdFrame ) this._gtdFrame = this._gtdFrame[1];	// a unique id	
	
	this.unsafeWindow.GTDCallBack = GIPROTOTYPE.bind(this, function(g){
		this._gmail = g;
		this.viewChange(g);
		
	});
	this.unsafeWindow.GTDInterceptEval = GIPROTOTYPE.bind(this, function(a){
		if( !((typeof a == "object") && (typeof a.length == "number")) ) return;
		a = a[0];
		this.dataChange(a);
	});
	
	
	this.windowLoaded = false;
	window.addEventListener('load', GIPROTOTYPE.bind(this, function() {
		if( this.windowLoaded==false ){				
			this.unsafeWindow.gtdinboxFrame = this._gtdFrame? true:false;
			var elScript = this.doc.createElement("SCRIPT");
			elScript.src = "chrome://gtdinbox/content/GTDInboxLocal.js";
			this.doc.getElementsByTagName("HEAD")[0].appendChild(elScript);
			this.windowLoaded = true;
		}
	}), true);
	
}catch(e){
	if( GTDDEBUGMODE ) alert("gtdinboxpage constructor\n" + e.toString());
}
};

GTDInboxPage.prototype.init = function(){
try{
	if( !this._isInit ){			
	
			if( !this.unsafeWindow.console ){
				this.unsafeWindow.console = {log: function(){}, trace: function(){}};
			}
				
			if( !this._gmail || !this._gmail.getActiveViewElement ){
				if( GTDDEBUGMODE ) alert("gtdinboxPage GTDCallBack - could not find gmail");
				return;	// error
			}
			
			
			this.gmailDOM = new GmailDOM(this);
			this.gmailDOM.viewChange(this._gmail);
			
			this._isInit = true;
			if( this._gtdFrame ){	// Strip it of surrounding elements (its a popup)
				
				var el = this._gmail.getMastheadElement();
				el.style.display = "none";
				var el = this._gmail.getFooterElement();
				el.style.display = "none";
				var el = this._gmail.getNavPaneElement();
				el.style.display = "none";
				
				
				//if( !this.unsafeWindow.top._asyncFrames ) this.unsafeWindow.top._asyncFrames = {};
				//this.unsafeWindow.top._asyncFrames[gtdFrame] = true;
			}
			
			var elNav = this.gmailDOM.element("NavPaneElement", this._gmail);
			this._asyncPopuper = new AsyncPopuper(this, elNav.ownerDocument.body);
			//this._contactFilterPopuper = new ContactFilterPopuper(this, elNav.ownerDocument.body);
			this._threadList = new ThreadList(this);
			GIPROTOTYPE.decodeEntities("0", this.doc);	// initialise it
			this.addCSS(this.gmailDOM.element("document", this._gmail));
			this.addCSS(this.doc);
			this._environment = new Environment(this, this.gmailDOM.element("MastheadElement", this._gmail));
			this._labels = new Labels(this);
			this._labels.load(elNav);
			this._threadListUI = new ThreadListUI(this);
			this._threadListPopupUI = new ThreadListPopupUI(this, elNav.parentNode);
			this._composeUI = new ComposeUI(this);
			this._conversationUI = new ConversationUI(this);
			this._dashboard = new Dashboard(this);
			this._filterEnvironment = new FilterEnvironment(this);
			this._labelsUI = new LabelsUI(this);
			
			this._lightbox = new Lightbox(this, this.doc.body);
			
			if( this.gtdinboxBrowser.showMsg ){
				this._lightbox.setType( this.gtdinboxBrowser.showMsg );
				this._lightbox.active(true);
				this.gtdinboxBrowser.showMsg = null;	// ensure doesn't show again on reload
			}
			
			this._createAboutBox();
	}
}catch(e){
	if( GTDDEBUGMODE ) alert("init\n" + e.toString());
}
};
GTDInboxPage.prototype.attachToBrowser = function(){
	this.browser = gBrowser.getBrowserForDocument( this.doc );
	this.browser.gtdinboxPage = this;
};
GTDInboxPage.prototype.addCSS = function(doc){
	var elLink = this.doc.createElement("LINK");
	elLink.type = "text/css";
	elLink.href = "chrome://gtdinbox/skin/gtdinbox.css";
	elLink.rel = "stylesheet";
	doc.getElementsByTagName("HEAD")[0].appendChild( elLink );
};

GTDInboxPage.prototype._createAboutBox = function(){
	this.elAbout = this._gmail.addNavModule("GTDInbox");
	
	var el = this.doc.createElement("DIV");
		el.className = "gtdinbox-about";
	this.elAbout.appendChild( el );
	
	var elOptions = this.doc.createElement("DIV");
		elOptions.className = "options";
	elOptions.innerHTML = "Options";	// TODO language
	el.appendChild(elOptions);
	
	var elGettingStarted = this.doc.createElement("DIV");
		elGettingStarted.className = "options";
	elGettingStarted.innerHTML = "Getting Started";	// TODO language
	el.appendChild(elGettingStarted);
	
	var elDebug = this.doc.createElement("DIV");
		elDebug.className = "options";
	elDebug.innerHTML = "Debug";	// TODO language
	el.appendChild(elDebug);
		
	//this.eventObservers.observe(elOptions, "mouseup", this._showOptions, false);
	elOptions.addEventListener("mousedown", GIPROTOTYPE.bind(this, function(){this._showLb('options')}), true);
	elGettingStarted.addEventListener("mousedown", GIPROTOTYPE.bind(this, function(){this._showLb('gettingstarted')}), true);
	elDebug.addEventListener("mousedown", GIPROTOTYPE.bind(this, function(){this._showLb('debug')}), true);
};
GTDInboxPage.prototype._showLb = function(name){
try{
	if( !(typeof name=="string") ) return;
	this._lightbox.setType( name );
	this._lightbox.active(true);
}catch(e){
	if( GTDDEBUGMODE ) alert("gtdinboxpage _showOptions\n" + e.toString());
}
};


GTDInboxPage.prototype.view = function(name){
	name = name || "view";
	switch(name){
		case "gtdviewid":
			return this._viewid;
			break;
		case "view":
			return this._gmail.getActiveViewType();
			break;
	};
};

GTDInboxPage.prototype.viewChange = function(gmail){
try{
	if( !gmail ){
		if( GTDDEBUGMODE ) alert("gtdinboxpage viewChange\nCould not find gmail");
		return;
	}

	var viewType = gmail.getActiveViewType();
	this._gmail = gmail;
	
	if( !this._isInit ){
		if( viewType ){	// is loaded
			this.init();
		}else{	// not ready to load
			return;
		}
	}
	
	var elActive = gmail.getActiveViewElement();
	this._viewid = (new Date()).valueOf();
	elActive.setAttribute("gtdviewid", this._viewid);
	
	this.eventObservers.reset();
	this._labels.viewChange(gmail);
	this.gmailDOM.viewChange(gmail);
	
	switch(viewType){
		case "tl":	// threadlist
			this._threadListUI.viewChange( gmail );
			break;
		case "cv":	// conversation
			this._conversationUI.viewChange(gmail);
			break;
		case "co":	// compose
			this._composeUI.viewChange(gmail);
			break;
		case "ct":	// contacts
			this._filterEnvironment.viewChange(gmail);
			break;
		case "s":	// settings
			this._labelsUI.viewChange(gmail);
			break;
	}
}catch(e){
	if( GTDDEBUGMODE ) alert("viewChange\n" + e.toString());
}
};

GTDInboxPage.prototype.dataChange = function(a){
	try{
		if( this._threadList ) this._threadList.parseAjaxResponse(a);
		if( this._environment ) this._environment.parseAjaxResponse(a);
		//this._labels.parseAjaxResponse(a);
		
		// Execute a Refresh when all data objects loaded for first time (only for Inbox; useful for auto label)
		if( this._threadList ){
			if( !this._firstFullLoad && this._threadList._threads.length>0 && this._threadList._threads.length==this._threadListUI.rowCount ){
				this._firstFullLoad = true;
				this._threadListUI.refresh(null, true);
			}
		}
	}catch(e){
		if( GTDDEBUGMODE ) alert("dataChange\n" + e.toString());
	}
};



GTDInboxPage.prototype.search = function(str){
		var elInput = this.gmailDOM.element("search-box");
	var elSubmit = this.gmailDOM.element("search-button");
	
	elInput.value = str;
	GIPROTOTYPE.doMouseEvent( this.doc, elSubmit, "click");
};


GTDInboxPage.prototype.unload = function(){
	// Clear up variables
	try{
	
	if( !this.doc && !this.browser ) return;

	if( this._threadList ) this._threadList.destroy();
	if( this._labels ) this._labels.destroy();
	if( this._threadListUI ) this._threadListUI.destroy();
	if( this.db ) this.db.destroy();
	this.eventObservers.destroy();
	if( this._asyncPopuper ) this._asyncPopuper.destroy();
	if( this._contactFilterPopuper ) this._contactFilterPopuper.destroy();
	if( this._filterEnvironment ) this._filterEnvironment.destroy();
	if( this._environment ) this._environment.destroy();
	if( this._composeUI ) this._composeUI.destroy();
	if( this._conversationUI ) this._conversationUI.destroy();
	if( this._lightbox ) this._lightbox.destroy();
	if( this._dashboard ) this._dashboard.destroy();

	this.browser.gtdinboxPage = null;
	delete this.browser.gtdinboxPage;
	
	this.unsafeWindow = null;
	this.gtdinboxBrowser = null;
	this.window = null;
	this.tab = null;
	this.browser = null;
	this.doc = null;

	}catch(e){if( GTDDEBUGMODE ) alert("gtdinboxPage unload\n" + e.toString())}
};

